package com.hhhn.wk.main.tab4.allorder.adapter;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.hhhn.wk.R;
import com.hhhn.wk.main.tab4.allorder.activity.AppraiseActivity;
import com.hhhn.wk.main.tab4.allorder.activity.IssueEvaluateActivity;
import com.hhhn.wk.main.tab4.allorder.activity.StarSendGoosActivity;
import com.hhhn.wk.main.tab4.allorder.activity.StartEvaluateActivity;
import com.hhhn.wk.main.tab4.allorder.activity.StartPaymentActivity;
import com.hhhn.wk.main.tab4.allorder.activity.StartReceiveActivity;
import com.hhhn.wk.main.tab4.allorder.entity.ItemGoodsBean;
import com.hhhn.wk.main.tab4.allorder.entity.MyOrder;

import com.hhhn.wk.pay.PayActivity;
import com.hhhn.wk.widget.ChildLiistView;

import java.util.ArrayList;
import java.util.List;
public class ParentAdapter extends BaseAdapter implements ListAdapter {
	private ArrayList<MyOrder> list;
	private Context context;
	private LayoutInflater inflater;
	private ChildAdapter daAdapter;
	public ParentAdapter(ArrayList<MyOrder> list, Context context) {
		super();
		this.list = list;
		this.context = context;

		this.inflater = LayoutInflater.from(context);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ChildListViewItem childListViewItem = null;
		if (convertView == null) {
			childListViewItem = new ChildListViewItem();
			convertView = inflater.inflate(R.layout.item_order_02, null, false);
			childListViewItem.tv_codeId = (TextView) convertView.findViewById(R.id.tv_codeId);
			childListViewItem.tv_pay_right = (TextView) convertView.findViewById(R.id.tv_pay_right);
			childListViewItem.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
			childListViewItem.parent_lv = (ChildLiistView) convertView.findViewById(R.id.parent_lv);
			childListViewItem.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
			childListViewItem.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			childListViewItem.ll_btn =  convertView.findViewById(R.id.ll_btn);
			convertView.setTag(childListViewItem);
		} else {
			childListViewItem = (ChildListViewItem) convertView.getTag();
		}
		childListViewItem.tv_codeId.setText(list.get(position).getOrder_codeId());
		childListViewItem.tv_money.setText("¥"+list.get(position).getActualPayment());
		childListViewItem.tv_time.setText(list.get(position).getOrder_time());
//        Log.d("@@", "加载: "+list.get(position).getOrder_status());
		childListViewItem.parent_lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int order_status = list.get(position).getOrder_status();
                Log.d("@@", "点击: "+order_status);
				switch (list.get(position).getOrder_status()){
                    /**
					 * 待支付
					 */
					case 103504:
								//待付款的详情
								Intent intent103504=new Intent(context,StartPaymentActivity.class);
								intent103504.putExtra("id",list.get(position).getId());
								context.startActivity(intent103504);
						break;
					/**
					 * 待发货
					 */
					case 103501:
								//待发货的详情
								Intent intent103501=new Intent(context, StarSendGoosActivity.class);
								intent103501.putExtra("id",list.get(position).getId());
								context.startActivity(intent103501);
						break;
					/**
					 * 待收货
					 */
					case 103502:
								Intent intent103502=new Intent(context, StartReceiveActivity.class);
								intent103502.putExtra("id",list.get(position).getId());
								context.startActivity(intent103502);
						break;
					/**
					 * 待评价
					 */
					case 103508:
						//待评价 的详情
						Intent intent103508=new Intent(context, AppraiseActivity.class);
						intent103508.putExtra("id",list.get(position).getId());
						context.startActivity(intent103508);
						break;
					/**
					 * 已完成
					 */
					case 103503:
								//已完成 的详情
								Intent intent103503=new Intent(context, StartEvaluateActivity.class);
								intent103503.putExtra("id",list.get(position).getId());
								context.startActivity(intent103503);
						break;
					default:
						break;
			}
			}
		});
		switch (list.get(position).getOrder_status()){
			//0全部  103504：待付款    103501：待发货   103502：待收货   103503：已完成
			//                       NEW
			// NOT_PAY("待付款", 103504),
			// SUCCESS("待发货", 103501),
			// TRANSPORT("已发货(待收货)",103502),
//          	FINISH("已完成", 103503),
// 				CANCEL("已取消", 103505)
// 				REFUND("退款中",103506),
//              CLOSE("已关闭", 103507),
// 				EVALUATE("已评价", 103508)
			/**
			 * 待支付
			 */
			case 103504:
				childListViewItem.tv_type.setText("待支付");
				childListViewItem.tv_type.setTextColor(R.color.colorAccent);
				childListViewItem.tv_pay_right.setText("去付款");

				childListViewItem.tv_pay_right.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View view) {
						//待付款的详情
						Intent intent=new Intent(context,StartPaymentActivity.class);
						intent.putExtra("id",list.get(position).getId());
						context.startActivity(intent);
					}
				});

				break;
			/**
			 * 待发货
			 */
			case 103501:
                //   order_type  103301 普通订单，103302 退款维权订单
                switch (list.get(position).getOrder_type()){
                    case 103301:
                        childListViewItem.tv_type.setText("待发货");
						childListViewItem.tv_type.setTextColor(R.color.colorAccent);
                        break;
                    case 103302:
                        childListViewItem.tv_type.setText("待发货(含退款)");
						childListViewItem.tv_type.setTextColor(R.color.colorAccent);
                        break;
                }
				childListViewItem.ll_btn.setVisibility(View.GONE);
				break;
			/**
			 * 待收货
			 */
			case 103502:
                //   order_type  103301 普通订单，103302 退款维权订单
                switch (list.get(position).getOrder_type()){
                    case 103301:
                        childListViewItem.tv_type.setText("待收货");
						childListViewItem.tv_type.setTextColor(R.color.colorAccent);
                        break;
                    case 103302:
                        childListViewItem.tv_type.setText("待收货(含退款)");
						childListViewItem.tv_type.setTextColor(R.color.colorAccent);
                        break;
                }
				childListViewItem.tv_pay_right.setText("确认收货");
				childListViewItem.tv_pay_right.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View view) {
						//待收货的详情
						Intent intent=new Intent(context, StartReceiveActivity.class);
						intent.putExtra("id",list.get(position).getId());
						context.startActivity(intent);
					}
				});
				break;
			/**
			 * 待评价
			 */
			case 103508:
				//   order_type  103301 普通订单，103302 退款维权订单
				switch (list.get(position).getOrder_type()){
					case 103301:
						childListViewItem.tv_type.setText("待评价");
						childListViewItem.tv_type.setTextColor(R.color.colorAccent);
						break;
					case 103302:
						childListViewItem.tv_type.setText("待评价(含退款)");
						childListViewItem.tv_type.setTextColor(R.color.colorAccent);
						break;
				}
				childListViewItem.tv_pay_right.setText("去评价");
				childListViewItem.tv_pay_right.setVisibility(View.VISIBLE);
				childListViewItem.tv_pay_right.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View view) {
						//待评价 的详情
						Intent intent103508=new Intent(context, AppraiseActivity.class);
						intent103508.putExtra("id",list.get(position).getId());
						context.startActivity(intent103508);
					}
				});
				break;
			/**
			 * 已完成
			 */
			case 103503:
                //   order_type  103301 普通订单，103302 退款维权订单
                switch (list.get(position).getOrder_type()){
                    case 103301:
                        childListViewItem.tv_type.setText("已完成");
						childListViewItem.tv_type.setTextColor(R.color.colorAccent);
                        break;
                    case 103302:
                        childListViewItem.tv_type.setText("已完成(含退款)");
						childListViewItem.tv_type.setTextColor(R.color.colorAccent);
                        break;
                }
				childListViewItem.tv_pay_right.setText("已完成");
				childListViewItem.tv_pay_right.setVisibility(View.GONE);
				break;
			/**
			 * 已取消
			 */
			case 103505:
				childListViewItem.tv_type.setText("已取消");
                childListViewItem.tv_type.setTextColor(context.getResources().getColor(R.color.color999));
				childListViewItem.tv_pay_right.setVisibility(View.GONE);
				break;
			/**
			 * 退款中
			 */
			case 103506:
				childListViewItem.tv_type.setText("退款中");
				childListViewItem.tv_pay_right.setVisibility(View.GONE);
				break;
			/**
			 * 已关闭
			 */
			case 103507:
				childListViewItem.tv_type.setText("已关闭");
				childListViewItem.tv_type.setTextColor(context.getResources().getColor(R.color.color999));
				childListViewItem.tv_pay_right.setVisibility(View.GONE);
				break;

		}
//		int z = ((List<ItemGoodsBean>)list.get(position).getBeanGoodsList()).size();
//		Log.d("适配器", "getView: "+list.get(position).getBeanGoodsList().toString());
//		if (z <= 2) {
////			bt_jz.setVisibility(View.GONE);
		daAdapter = new ChildAdapter(context);
		daAdapter.addAll(((List<ItemGoodsBean>)list.get(position).getBeanGoodsList()));
		childListViewItem.parent_lv.setAdapter(daAdapter);
//		childListViewItem.parent_lv.setOnItemClickListener(new OnItemClickListener() {
//					@Override
//					public void onItemClick(AdapterView<?> arg0, View arg1,
//											int arg2, long arg3) {
//						Intent intent = new Intent(context, Activity_2.class);
//						context.startActivity(intent);
//					}
//				});
		return convertView;
	}
	public class ChildListViewItem {
		TextView tv_codeId;//单号
		TextView tv_type;//状态
		TextView tv_money;//价额
		TextView tv_time;//时间
		TextView tv_pay_right;//右边  按钮
		ChildLiistView parent_lv;
		LinearLayout ll_btn;
	}

}
